﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using Microsoft.Phone.Shell;
using VodafoneWP7.Resources;

namespace VodafoneWP7
{
    public partial class CodeVerificationPage : PhoneApplicationPage
    {
        MainPageStatus status;
        //bool stopped = false;
        Account SelectedAccount;
        public CodeVerificationPage()
        {
            InitializeComponent();
            SelectedAccount = null;
        }

        private void CodeVerificationPage_Loaded(object sender, RoutedEventArgs e)
        {
            ApplicationBarIconButton Close_Button, Send_Button;

            if (App.CurrentTheme == App.Theme.Dark)
            {
                Send_Button = new ApplicationBarIconButton(new Uri("\\Images\\dark\\appbar.feature.email.rest.png", UriKind.Relative)) { Text = Strings.Send, IsEnabled = true };
                Close_Button = new ApplicationBarIconButton(new Uri("\\Images\\dark\\appbar.close.rest.png", UriKind.Relative)) { Text = Strings.Cancel, IsEnabled = true };
            }
            else
            {
                Send_Button = new ApplicationBarIconButton(new Uri("\\Images\\light\\appbar.feature.email.rest.png", UriKind.Relative)) { Text = Strings.Send, IsEnabled = true };
                Close_Button = new ApplicationBarIconButton(new Uri("\\Images\\light\\appbar.close.rest.png", UriKind.Relative)) { Text = Strings.Cancel, IsEnabled = true };
            }
            Send_Button.Click += Send_Button_Click;
            Close_Button.Click += Close_Button_Click;

            ApplicationBar.Buttons.Add(Close_Button);
            ApplicationBar.Buttons.Add(Send_Button);
            Image.Stretch = Stretch.Uniform;
            Image.Source = MainPage.bitmapImage;
            Image.Visibility = System.Windows.Visibility.Visible;
        }


        private void Close_Button_Click(object sender, EventArgs e)
        {
            DisableProgressBar();
            NavigationService.GoBack();

        }

        private void Send_Button_Click(object sender, EventArgs e)
        {
            EnableProgressBar();
            //MainPage.currentMessageService.SubmitVerificationCode(Code_Area.Text, Image);
            if (SelectedAccount == null)
                NavigationService.GoBack();
            else
                //SelectedAccount.messaggingService.SubmitVerificationCode(Code_Area.Text, Image, status, ErrorInTask, TaskCompleted, Dispatcher);
                MessaggingServices.GetService(SelectedAccount.MessagingService).SubmitVerificationCode(Code_Area.Text, Image, status, ErrorInTask, TaskCompleted, Dispatcher);
            //sendbutton.IsEnabled = false;

        }
        private void ErrorInTask(String s)
        {
            MessageBox.Show(s);
        }
        private void TaskCompleted()
        {
            for (int i = 0; i < MainPage.Accounts.Count; i++)
            {
                var a = MainPage.Accounts[i];
                if (a.Name == SelectedAccount.Name && a.Username == SelectedAccount.Username && a.MessagingService == SelectedAccount.MessagingService)
                {
                    a.messages.Add(new Message() { type = MessageType.SMS, text = status.message, dateTime = DateTime.Now, number = status.number });
                    break;
                }
            }

            //SelectedAccount.myMessages.Add(new Message() { type = MessageType.SMS, text = status.message, dateTime = DateTime.Now, number = status.number });
            Storage.SaveAccounts(MainPage.Accounts);
            NavigationService.GoBack();
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            try
            {



                //if (PhoneApplicationService.Current.State.ContainsKey("MainPageStatus"))
                //{
                status = (MainPageStatus)(PhoneApplicationService.Current.State["MainPageStatus"]);
                SelectedAccount = (Account)PhoneApplicationService.Current.State["SelectedAccount"];

                //this.State.Remove("MainPageStatus");
                //}
                //else
            }
            catch (Exception ex)
            {
                ex.ToString();
                NavigationService.GoBack();
            }
        }
        protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            base.OnNavigatingFrom(e);
            //stopped = true;
            DisableProgressBar();
        }

        #region PROGRESS_BAR

        private void EnableProgressBar()
        {
            ToggleProgressBar.IsEnabled = true;
            ToggleProgressBar.IsIndeterminate = true;
            ToggleProgressBar.Visibility = System.Windows.Visibility.Visible;
        }
        private void DisableProgressBar()
        {
            ToggleProgressBar.IsEnabled = false;
            ToggleProgressBar.IsIndeterminate = false;
            ToggleProgressBar.Visibility = System.Windows.Visibility.Collapsed;
        }
        private void DisableProgressBar(string message)
        {
            MessageBox.Show(message);
            ToggleProgressBar.IsEnabled = false;
            ToggleProgressBar.IsIndeterminate = false;
            ToggleProgressBar.Visibility = System.Windows.Visibility.Collapsed;
        }

        #endregion

    }

}